﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Dashboard.Keyboard;
using System.Runtime.InteropServices;

namespace Dashboard
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        //public App() {
        //    var window = new MainWindow();
        //    this.MainWindow = window;

        //    window.Show();

        //}

        KeyboardListener KListener = new KeyboardListener();

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            KListener.KeyDown += new RawKeyEventHandler(KListener_KeyDown);
        }
        
        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(int keyCode);

        public enum Keys
        {
            Left = 37,
            Up = 38,
            Right = 39,
            Down = 40,
            Control = 17,
            Space = 32,
            Shift = 16,
            c=67
        }

        public static bool IsKeyDown(Keys k)
        {
            return (GetAsyncKeyState((int)k) & 0x8000) != 0;
        }

        void KListener_KeyDown(object sender, RawKeyEventArgs args)
        {
            if (IsKeyDown(Keys.Control) && IsKeyDown(Keys.Shift) && IsKeyDown(Keys.c))
            {
                //Console.WriteLine("ctrol+shift+c");
                System.Threading.Thread thread = new System.Threading.Thread(
                new System.Threading.ThreadStart(
                  delegate()
                  {
                      //this.Dispatcher.Invoke();
                      this.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Input,
                        new Action(
                          delegate()
                          {
                              this.MainWindow.Activate();
                              var main = App.Current.MainWindow as _MyDashboard; // If not a static method, this.MainWindow would work
                              main.pastecontent();
                          }
                      ));
                  }
                ));
                thread.Priority = System.Threading.ThreadPriority.Highest;
                thread.Start();     
            
            }
            //if(IsKeyDown(Keys.Control) && IsKeyDown(Keys.Space))
            //{
            //    System.Threading.Thread thread = new System.Threading.Thread(
            //    new System.Threading.ThreadStart(
            //      delegate()
            //      {
            //          //this.Dispatcher.Invoke();
            //          this.Dispatcher.Invoke(
            //            System.Windows.Threading.DispatcherPriority.Input,
            //            new Action(
            //              delegate()
            //              {
            //                  this.MainWindow.Activate();
            //                  Console.WriteLine("activated");
            //              }
            //          ));
            //      }
            //    ));
            //    thread.Priority = System.Threading.ThreadPriority.Highest;
            //    thread.Start();                 
            //}
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            KListener.Dispose();
        }


    }

}
